package com.yrmao.chiefenergyofficer.ui.fragment.itemcontent;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yrmao.chiefenergyofficer.R;
import com.yrmao.chiefenergyofficer.ui.modle.GridviewContentInfo;

import java.util.List;

/**
 * Created by yemao on 16/11/30.
 */

public class ContentGridLayout extends LinearLayout implements AdapterView.OnItemClickListener {
    private GridView mGridView;
    private View mcontentview;
    private List<GridviewContentInfo> list;

    public ContentGridLayout(Context context, List<GridviewContentInfo> list, int pagesize) {
        super(context);
        this.list = list;
        mcontentview = LayoutInflater.from(context).inflate(R.layout.content_gridlayout, null);
        mGridView = (GridView) mcontentview.findViewById(R.id.gridview_content);
        mGridView.setNumColumns(pagesize / 2);
        mGridView.setAdapter(new MAdapter(context, list));
        mGridView.setSelector(new ColorDrawable());
        addView(mcontentview);
        mGridView.setOnItemClickListener(this);
    }

    private String[] content = {"红包助手", "地图", "公交线路", "待定1", "待定2", "待定3"};

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if (list.get(i).getContentValue().equals("红包助手"))
            switch (list.get(i).getContentValue()) {
                case "红包助手":
                    break;
                case "地图":
                    break;
                case "公交线路":
                    break;
                case "待定1":
                    break;
                case "待定2":
                    break;
                case "待定3":
                    break;
            }

    }


    private class MAdapter extends BaseAdapter {
        private Context context;
        private List<GridviewContentInfo> list;

        public MAdapter(Context context, List<GridviewContentInfo> list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list == null ? 0 : list.size();
        }

        @Override
        public Object getItem(int i) {
            return list == null ? 0 : list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View contentview, ViewGroup viewGroup) {
            ViewHolder viewHolder = null;
            contentview = LayoutInflater.from(context).inflate(R.layout.item_gridview_content, null);
            viewHolder = new ViewHolder(contentview);
            viewHolder.mImage.setImageResource(list.get(i).getImage());
            viewHolder.mTitle.setText(list.get(i).getContentValue());
            return contentview;
        }

        private class ViewHolder {
            TextView mTitle;
            ImageView mImage;

            public ViewHolder(View view) {
                mTitle = (TextView) view.findViewById(R.id.tv_itme_grid_content_title);
                mImage = (ImageView) view.findViewById(R.id.iv_item_grid_content_img);
            }
        }
    }
}
